﻿using System;
using System.Configuration;
using System.Web.Configuration;
using System.Collections.ObjectModel;

using System.Web.Security;

namespace VioletDevelopmentLaboratory.Net.Web.Configuration
{
	public sealed class MembershipSectionManager : ConfigurationManagerBase
	{
		private MembershipSection section;

		public MembershipSectionManager()
		{
			this.section = (MembershipSection)base.GetConfigurationSection("system.web/membership");
		}

		public void Modify(string defaultProvider, TimeSpan userIsOnlineTimeWindow, string hashAlgorithmType)
		{
			if (string.IsNullOrEmpty(defaultProvider))
			{
				defaultProvider = "AspNetSqlProvider";
			}
			if (string.IsNullOrEmpty(hashAlgorithmType))
			{
				hashAlgorithmType = "";
			}
			this.section.DefaultProvider = defaultProvider;
			this.section.UserIsOnlineTimeWindow = userIsOnlineTimeWindow;
			this.section.HashAlgorithmType = hashAlgorithmType;
			this.Save();
		}
		public MembershipSection GetMembership()
		{
			return this.section;
		}




		public void AddProvider(RequiredConnectionStringNameProviderSettings provider)
		{
			RequiredConnectionStringNameProviderSettings.Add(
				this.section.Providers,
				provider
			);
			this.Save();
		}
		public void ModifyProvider(RequiredConnectionStringNameProviderSettings provider)
		{
			RequiredConnectionStringNameProviderSettings.Modify(
				this.section.Providers,
				provider
			);
			this.Save();
		}
		public void RemoveProvider(RequiredConnectionStringNameProviderSettings provider)
		{
			RequiredConnectionStringNameProviderSettings.Remove(
				this.section.Providers,
				provider
			);
			this.Save();
		}
		public void ClearProviders()
		{
			this.section.Providers.Clear();

			this.Save();
		}
		public RequiredConnectionStringNameProviderSettings GetProvider(string name)
		{
			return RequiredConnectionStringNameProviderSettings.ConvertToRequiredConnectionStringNameProviderSettings(
				this.section.Providers[name]
			);
		}
		public Collection<RequiredConnectionStringNameProviderSettings> GetProviders()
		{
			Collection<RequiredConnectionStringNameProviderSettings> roleProviders = new Collection<RequiredConnectionStringNameProviderSettings>();

			foreach (ProviderSettings provider in this.section.Providers)
			{
				roleProviders.Add(
					RequiredConnectionStringNameProviderSettings.ConvertToRequiredConnectionStringNameProviderSettings(provider)
				);
			}

			return roleProviders;
		}
	}

	public sealed class MembershipSectionManagerForSql : ConfigurationManagerBase
	{
		private MembershipSection section;

		public MembershipSectionManagerForSql()
		{
			this.section = (MembershipSection)base.GetConfigurationSection("system.web/membership");
		}

		public void AddProvider(SqlMembershipProviderSettings provider)
		{
			SqlMembershipProviderSettings.Add(
				this.section.Providers,
				provider
			);

			this.Save();
		}
		public void ModifyProvider(SqlMembershipProviderSettings provider)
		{
			SqlMembershipProviderSettings.Modify(
				this.section.Providers,
				provider
			);
			this.Save();
		}
		public void RemoveProvider(SqlMembershipProviderSettings provider)
		{
			SqlMembershipProviderSettings.Remove(
				this.section.Providers,
				provider
			);
			this.Save();
		}
		public void ClearProviders()
		{
			this.section.Providers.Clear();

			this.Save();
		}
		public SqlMembershipProviderSettings GetProvider(string name)
		{
			return SqlMembershipProviderSettings.ConvertToSqlMembershipProviderSettings(
				this.section.Providers[name]
			);
		}
		public Collection<SqlMembershipProviderSettings> GetProviders()
		{
			Collection<SqlMembershipProviderSettings> providers = new Collection<SqlMembershipProviderSettings>();

			foreach (ProviderSettings provider in this.section.Providers)
			{
				providers.Add(
					SqlMembershipProviderSettings.ConvertToSqlMembershipProviderSettings(provider)
				);
			}

			return providers;
		}
	}
}
